package first.group.ssh.packet.writer;

import first.group.ssh.packet.RawPacket;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class StringWriter {
    public static void writeByteBuffer(ByteBuffer buffer, ByteBuffer data) {
        buffer.putInt(data.limit());
        buffer.put(data);
    }
    
    public static void writePacket(ByteBuffer buffer, RawPacket packet) {
        var payload = packet.getPayload();
        buffer.putInt(payload.length + 1);
        buffer.put((byte) packet.getType());
        buffer.put(payload);
    }
    
    public static void writeByteArray(ByteBuffer buffer, byte[] array) {
        buffer.putInt(array.length);
        buffer.put(array);
    }
    
    public static void writeString(ByteBuffer buffer, String str) {
        writeByteBuffer(buffer, StandardCharsets.UTF_8.encode(str));
    }
}
